﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace AB.Common
{
    /// <summary>
    /// SqlParameter
    /// </summary>
    public static class SqlParameterHelper
    {
        /// <summary>
        /// 设置该参数的值
        /// </summary>
        /// <param name="sqlParameter"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static SqlParameter Value(this SqlParameter sqlParameter, object obj)
        {
            sqlParameter.Value = obj;
            return sqlParameter;
        }
        /// <summary>
        /// 设置一个值，该值指示参数是只可输入、只可输出、双向还是存储过程返回值参数。
        /// </summary>
        /// <param name="sqlParameter"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        public static SqlParameter Direction(this SqlParameter sqlParameter, ParameterDirection direction)
        {
            sqlParameter.Direction = direction;
            return sqlParameter;
        }
        /// <summary>
        /// 获取该参数的值
        /// </summary>
        /// <param name="sqlParameters"></param>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public static object GetValue(this SqlParameter[] sqlParameters, string parameterName)
        {
            if (sqlParameters != null)
            {
                List<SqlParameter> list = sqlParameters.ToList();
                SqlParameter p = list.Find(e => e.ParameterName == parameterName);
                if (p != null)
                {
                    return p.Value;
                }
            }
            return null;
        }
    }
}
